module Algorithm_m
contains
subroutine sort_selection(A, isAsc)
    implicit none
    integer, intent(inout) :: A(:)
    logical, intent(in) :: isAsc

    integer :: i, j, idx, temp, current, N
    N = size(A, 1)
    if (isAsc) then
        do i = 1, N
            current = A(i)
            idx = i
            do j = i + 1, N
                if (A(j) < current) then
                    current = A(j); idx = j
                end if
            end do
            if (idx /= i) then
                temp = A(idx); A(idx) = A(i); A(i) = temp
            end if
        end do
    else
        do i = 1, N
            current = A(i)
            idx = i
            do j = i + 1, N
                if (A(j) > current) then
                    current = A(j); idx = j
                end if
            end do
            if (idx /= i) then
                temp = A(idx); A(idx) = A(i); A(i) = temp
            end if
        end do        
    end if
end subroutine
end module
